﻿#include "timgwidget.h"

//TImgWidget::TImgWidget() {}
#include <QDebug>
#include <QGraphicsSceneMouseEvent>
#include <QPointF>
#include <QGraphicsSceneDragDropEvent>
#include <QDrag>
#include <math.h>

TImgWidget::TImgWidget(QPixmap *pixmap)
{
    m_pix = *pixmap;
    setAcceptDrops(true);//If enabled is true, this item will accept hover events; otherwise, it will ignore them. By default, items do not accept hover events.
    m_scaleValue = 0;
    m_scaleDafault = 0;
    m_isMove = false;
    m_minScale = 0;
    m_isFlippedX = false;
    m_isFlippedY = false;
}
QRectF TImgWidget::boundingRect() const
{
    return QRectF(-m_pix.width() / 2, -m_pix.height() / 2,
                  m_pix.width(), m_pix.height());
}

void TImgWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
    painter->drawPixmap(-m_pix.width() / 2, -m_pix.height() / 2, m_pix);
}

void TImgWidget::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        setCursor(Qt::ClosedHandCursor); // 按下时设置为手型指针
    }

    if (event->button() == Qt::LeftButton) {
        QPointF pos = event->pos();
        if (m_isFlippedX) {
            pos.setX(-1 * pos.x()); // 水平翻转时调整 X 坐标
        }
        if (m_isFlippedY) {
            pos.setY(-1 * pos.y()); // 垂直翻转时调整 Y 坐标
        }
        m_startPos = pos;
        m_isMove = true;
    } else if (event->button() == Qt::RightButton) {
        ResetItemPos();
    }
}

void TImgWidget::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    if (m_isMove) {
        QPointF pos = event->pos();
        if (m_isFlippedX) {
            pos.setX(-1 * pos.x()); // 水平翻转时调整 X 坐标
        }
        if (m_isFlippedY) {
            pos.setY(-1 * pos.y()); // 垂直翻转时调整 Y 坐标
        }
        QPointF point = (pos - m_startPos) * m_scaleValue;
        moveBy(point.x(), point.y());
    }
}

void TImgWidget::mouseReleaseEvent(QGraphicsSceneMouseEvent *)
{
    setCursor(Qt::OpenHandCursor); // 松开时设置为手型指针
    m_isMove = false;//标记鼠标左键已经抬起
}


void TImgWidget::wheelEvent(QGraphicsSceneWheelEvent *event)//鼠标滚轮事件
{
    if((event->delta() > 0)&&(m_scaleValue >= 50))//最大放大到原始图像的50倍
    {
        return;
    }
    else if((event->delta() < 0)&&(m_scaleValue <= m_minScale))//图像缩小到自适应大小之后就不继续缩小
    {
        //ResetItemPos();//重置图片大小和位置，使之自适应控件窗口大小
    }
    else
    {
        qreal qrealOriginScale = m_scaleValue;
        if(event->delta() > 0)//鼠标滚轮向前滚动
        {
            m_scaleValue*=1.1;//每次放大10%
        }
        else
        {
            m_scaleValue*=0.9;//每次缩小10%
        }
        setScale(m_scaleValue);

        // 这是处理翻转的情况
        int x = event->pos().x();
        int y = event->pos().y();
        if (m_isFlippedX) {
            x = x * -1;
        }
        if (m_isFlippedY) {
            y = y * -1;
        }

        if(event->delta() > 0)
        {
            moveBy(-x*qrealOriginScale*0.1, -y*qrealOriginScale*0.1);//使图片缩放的效果看起来像是以鼠标所在点为中心进行缩放的
        }
        else
        {
            moveBy(x*qrealOriginScale*0.1, y*qrealOriginScale*0.1);//使图片缩放的效果看起来像是以鼠标所在点为中心进行缩放的
        }
    }
}

void TImgWidget::setQGraphicsViewWH(int nwidth, int nheight)//将主界面的控件QGraphicsView的width和height传进本类中，并根据图像的长宽和控件的长宽的比例来使图片缩放到适合控件的大小
{
    int nImgWidth = m_pix.width();
    int nImgHeight = m_pix.height();
    qreal temp1 = nwidth*1.0/nImgWidth;
    qreal temp2 = nheight*1.0/nImgHeight;
    if(temp1>temp2)
    {
        m_scaleDafault = temp2;
    }
    else
    {
        m_scaleDafault = temp1;
    }
    setScale(m_scaleDafault);
    m_scaleValue = m_scaleDafault;
}

void TImgWidget::setMinScale(int width, int height)
{
    int nImgWidth = m_pix.width();
    int nImgHeight = m_pix.height();
    qreal temp1 = width * 1.0 / nImgWidth;
    qreal temp2 = height * 1.0 / nImgHeight;
    if(temp1>temp2)
    {
        m_minScale = temp2;
    }
    else
    {
        m_minScale = temp1;
    }
}

void TImgWidget::setMax()
{
    if(m_scaleValue >= 50)//最大放大到原始图像的50倍
    {
        return;
    }

    m_scaleValue*=1.1;//每次放大10%
    setScale(m_scaleValue);
}

void TImgWidget::setMin()
{
    if (m_scaleValue <= m_minScale)//图像缩小到自适应大小之后就不继续缩小
    {
        return;
    }

    m_scaleValue*=0.9;//每次缩小10%
    setScale(m_scaleValue);
}

void TImgWidget::ResetItemPos()//重置图片位置
{
    m_scaleValue = m_scaleDafault;//缩放比例回到一开始的自适应比例
    setScale(m_scaleDafault);//缩放到一开始的自适应大小
    setPos(0,0);
}

qreal TImgWidget::getScaleValue() const
{
    return m_scaleValue;
}


void TImgWidget::setFlippedX(bool flipped) {
    m_isFlippedX = flipped;
}

bool TImgWidget::getFlippedX() const
{
    return m_isFlippedX;
}

void TImgWidget::setFlippedY(bool flipped) {
    m_isFlippedY = flipped;
}

bool TImgWidget::getFlippedY() const
{
    return m_isFlippedY;
}

